<template>
  <div class="mobile-side-nav" :class="{ 'is-open': isOpen }">
    <!-- 遮罩层 -->
    <div class="side-nav-overlay" @click="close"></div>

    <!-- 侧边栏内容 -->
    <div class="side-nav-content">
      <!-- 头部 -->
      <div class="side-nav-header">
        <button class="close-btn" @click="close">
          <i class="fas fa-times"></i>
        </button>
        <div class="user-info" @click="handleUserClick">
          <img v-if="userStore.userInfo?.member?.face" :src="userStore.userInfo?.member?.face" alt="" style="width: 30px;height: 30px;border-radius: 50%"></img>
          <i v-else class="fas fa-user-circle"></i>
          <span>{{ $t('您好') }}，{{ userStore.isLogin ? userStore.userInfo?.member?.nickName : $t('请登录') }}</span>
        </div>
      </div>

      <!-- 导航菜单 -->
      <div class="side-nav-menu">
        <!-- 语言选择部分 -->
        <div class="menu-section">
          <h3 class="menu-title">{{ $t('语言设置') }}</h3>
          <ul class="menu-list">
            <li>
              <router-link to="/language" @click="close">
                <i class="fas fa-globe"></i>
                {{ langStore.languageName }} - {{ langStore.language }}
                <span class="selected-mark">✓</span>
              </router-link>
            </li>
          </ul>
        </div>

        <div class="menu-section">
          <h3 class="menu-title">{{ $t('热门活动') }}</h3>
          <ul class="menu-list">
            <li>
              <router-link to="/deals" @click="close">
                <i class="fas fa-bolt"></i>
                {{ $t('限时秒杀') }}
              </router-link>
            </li>
            <li>
              <router-link to="/about-coupons" @click="close">
                <i class="fas fa-ticket-alt"></i>
                {{ $t('关于优惠卷') }}
              </router-link>
            </li>
          </ul>
        </div>

        <div class="menu-section">
          <h3 class="menu-title">{{ $t('商品分类') }}</h3>
          <ul class="menu-list">
            <li v-for="v in cartStore.fatherCategoryList" :key="v.id">
              <a @click="goCategory(v)">
                <img :src="v.image" alt="" style="width: 20px;height: 20px">
                {{ v.name }}
              </a>
            </li>
          </ul>
        </div>

        <div class="menu-section">
          <h3 class="menu-title">{{ $t('账户管理') }}</h3>
          <ul class="menu-list">
            <li>
              <router-link to="/account" @click="close">
                <i class="fas fa-user"></i>
                {{ $t('我的账户') }}
              </router-link>
            </li>
            <li>
              <router-link to="/account/orders" @click="close">
                <i class="fas fa-shopping-bag"></i>
                {{ $t('我的订单') }}
              </router-link>
            </li>
            <li>
              <router-link to="/account/messages" @click="close">
                <i class="fas fa-envelope"></i>
                {{ $t('消息中心') }}
              </router-link>
            </li>
            <li>
              <router-link to="/account/customer-service" @click="close">
                <i class="fas fa-headset"></i>
                {{ $t('联系客服') }}
              </router-link>
            </li>
            <li v-if="userStore.isLogin">
              <a href="#" @click.prevent="handleLogout">
                <i class="fas fa-sign-out-alt"></i>
                {{ $t('退出登录') }}
              </a>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import useUserStore from '@/store/modules/user'
import useLangStore from "@/store/modules/lang.js";
import useCartStore from "@/store/modules/cart.js";

const isOpen = ref(false)
const userStore = useUserStore()
const router = useRouter()
const langStore = useLangStore()
const cartStore = useCartStore()

const handleUserClick = () => {
  if (!userStore.isLogin) {
    router.push('/login')
    isOpen.value = false
  }
}

const handleLogout = () => {
  userStore.logOut().then(() => {
    window.location.reload()
  })
  isOpen.value = false
}

const open = () => {
  isOpen.value = true
  document.body.style.overflow = 'hidden'
}

const close = () => {
  isOpen.value = false
  document.body.style.overflow = ''
}

const goCategory = (v) => {
  close()
  router.push({
    path: '/products/category',
    query: {
      catId: v.catId,
      name: v.name
    }
  })
}

defineExpose({
  open
})

onMounted(() => {
})

</script>

<style lang="scss" scoped>
.mobile-side-nav {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 2000;
  visibility: hidden;

  &.is-open {
    visibility: visible;

    .side-nav-overlay {
      opacity: 1;
    }

    .side-nav-content {
      transform: translateX(0);
    }
  }
}

.side-nav-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.side-nav-content {
  position: absolute;
  top: 0;
  left: 0;
  width: 85%;
  max-width: 360px;
  height: 100%;
  background: #fff;
  transform: translateX(-100%);
  transition: transform 0.3s ease;
  display: flex;
  flex-direction: column;
}

.side-nav-header {
  padding: 16px;
  background: $primary-color;
  color: #fff;
  position: relative;

  .close-btn {
    position: absolute;
    top: 16px;
    right: 16px;
    width: 32px;
    height: 32px;
    border: none;
    background: none;
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .user-info {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-top: 8px;
    cursor: pointer;

    i {
      font-size: 32px;
    }

    span {
      font-size: 16px;
    }
  }
}

.side-nav-menu {
  flex: 1;
  overflow-y: auto;
  padding: 16px 0;

  .menu-section {
    margin-bottom: 24px;

    &:last-child {
      margin-bottom: 0;
    }
  }

  .menu-title {
    padding: 0 16px;
    margin-bottom: 8px;
    font-size: 14px;
    color: #666;
    font-weight: normal;
  }

  .menu-list {
    list-style: none;
    padding: 0;
    margin: 0;

    li {
      a {
        display: flex;
        align-items: center;
        gap: 12px;
        padding: 12px 16px;
        color: #111;
        text-decoration: none;
        font-size: 15px;

        i {
          width: 20px;
          text-align: center;
          color: #555;
        }

        .selected-mark {
          margin-left: auto;
          color: #008a00;
          font-weight: bold;
        }

        &:hover,
        &:active {
          background: #f4f4f4;
        }
      }
    }
  }
}
</style>
